package com.base.cn.platform.os.controller.user.account;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.user.AccountBizType;
import com.base.cn.platform.os.common.utils.user.AccountHistoryType;
import com.base.cn.platform.os.service.manage.user.account.CusUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 后台账户
 *
 * @author ll
 * @create 2018-04-25
 */
@Controller
@RequestMapping("/admin/user/cus")
public class AdminCusUserAccountController extends AdminBaseController {

    @Autowired
    private CusUserAccountService cusUserAccountService;
    /**
     * 账户列表页面
     * @return
     */
    @RequestMapping("/toCusUserAccountPage")
    public String toCusUserAccountPage(){
        return resultPage("/user/account/students_sys_accountList");
    }

    /**
     * 搜索用户账户列表
     * @param model
     * @param userPamars
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxCusUserAccountList")
    public String ajaxCusUserAccountList(Model model,@RequestBody Map<String, Object> userPamars,
                                         @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        Map<String,Object> dataMap =cusUserAccountService.queryCusUserAccountListPage(userPamars,currentPage);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/user/account/students_ajax_sys_accountList");
    }


    /**
     * 操作账户金额
     * type=1银行充值，type=2银行冻结金额,type=3银行解冻金额
     * type=4充值卡入账,type=5充值卡冻结金额,type=6充值卡解冻金额
     * type=7银行扣款，type=8充值卡扣款
     * @param id 账户ID
     * @param type 操作类型
     * @param amount 操作金额
     * @return Map<String , Object>
     */

    @RequestMapping("/updateUserAccountAmount")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.USER, action = "操作学员账户金额")
    public Map<String, Object> updateUserAccountAmount(HttpServletRequest request,
                                                       @RequestParam("id")BigDecimal id,
                                                       @RequestParam("type") int type,
                                                       @RequestParam("amount") double amount){
        BigDecimal sysUserId = loginUtil.getLoginUserId(request);
        return cusUserAccountService.updateUserAccountAmount(sysUserId,id,type,amount);
    }

    /**
     * 修改账户状态
     * @param status  状态，ACTIVE正常，FROZEN冻结
     * @param id 账户 ID
     * @return Map<String , Object>
     */
    @RequestMapping("/updateUserAccountStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.USER, action = "修改学员账户状态")
    public Map<String,Object> updateUserAccountStatus(@RequestParam("status") String status,
                                                      @RequestParam("id") BigDecimal id){
        return cusUserAccountService.updateUserAccountStatus(status,id);
    }

    /**
     * 账户详情
     * @param model
     * @param userId
     * @return
     */
    @RequestMapping("/userAccountInfo")
    public String userAccountInfo(Model model,
                                  @RequestParam("userId")BigDecimal userId){
        Map<String,Object>  cusUserAccount =cusUserAccountService.userAccountInfo(userId,true);
        model.addAttribute("account",cusUserAccount);
        model.addAttribute("accountBizTypeList",AccountBizType.values());
        model.addAttribute("accountHistoryTypeList", AccountHistoryType.values());
        return resultPage("/user/account/students_sys_accountInfo");
    }
}
